import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import 'package:wk_robot2/app/robot_comm/mqtt_comm_manager.dart';
import 'package:wk_robot2/app/utils/CommonUtil.dart';
import 'package:wk_robot2/app/widget/CommonWidgets.dart';
import '../../../routes/app_pages.dart';
import '../../../widget/tips.dart';

class AboutAccountController extends GetxController {

  var userName = ''.obs;
  var bindPhoneNumber = ''.obs;

  int clickCount = 0;
  DateTime? lastClickTime;

  @override
  void onInit() {
    super.onInit();

    initData();
  }

  void initData() {
    Map loginUser = CommonUtil.getLoginUser();
    print('loginUser  $loginUser');
    userName.value = loginUser['nickName'];
    bindPhoneNumber.value = loginUser['userPhone'];
    bindPhoneNumber.value = maskPhoneNumber(bindPhoneNumber.value);
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    super.onClose();
  }

  void onAvatorClicked() {
    final now = DateTime.now();

    // 如果上次点击时间不为空且与当前时间差超过5秒，则重置计数
    if (lastClickTime != null && now.difference(lastClickTime!) > Duration(seconds: 5)) {
      clickCount = 0;
    }

    lastClickTime = now;
      clickCount++;
      if (clickCount >= 5) {
        Tips.showToast('userid: ${CommonUtil.getLoginUserId().toString()}');
        clickCount = 0; // 重置计数
      }
  }

  loginOut() {
    MqttCommManager().disconnectToServer();
    /*StorageManager.getStorage().remove(AppConst.storage_key_login_token);
    StorageManager.getStorage().remove(AppConst.storage_current_login_user);*/
    GetStorage().erase();
    Get.offAllNamed(Routes.LOGIN);


  }

  String maskPhoneNumber(String phoneNumber) {
    // 检查手机号长度是否合法（假设为11位）
    if (phoneNumber.length != 11) {
      return phoneNumber;
    }

    // 使用字符串切片和拼接来隐藏中间四位
    return "${phoneNumber.substring(0, 3)}****${phoneNumber.substring(7)}";
  }

}
